#include <stdio.h>
#include <string.h>
#include "_student_manager.h"
int student_count;
void clear() 
{
    char c;
    while ((c = getchar()) != '\n' && c != EOF);
}

void ADD_STU(stu* p, int len) 
{
    int x = 0;
    for (int i = 0; i < len; i++) {
        if (p[i].age == 0) {
            printf("Enter id: ");
            scanf("%d", &p[i].id);
            printf("Enter name: ");
            scanf("%s", p[i].name);
            printf("Enter age: ");
            scanf("%d", &p[i].age);
            printf("Enter tel: ");
            scanf("%s", p[i].tel);
            printf("Finish %s's information input\n", p[i].name);
            student_count++;
            printf("Enter 0 to continue adding, other key to return: ");
            scanf("%d", &x);
            if (x != 0) return;
        }
    }
    printf("Address book is full\n");
}

int FIND_STU(stu* p, int way, int len) 
{
    char name[10], tel[12];
    int id, age;
    int found_index = -1;
    int count = 0;

    switch (way)
     {
        case 1:
            printf("Enter id to find: ");
            scanf("%d", &id);
            for (int i = 0; i < len; i++)
            {
                if (p[i].age != 0 && p[i].id == id) 
                {
                    found_index = i;
                    count++;
                    if (count > 1) return -2;
                }
            }
            break;
        case 2:
            printf("Enter name to find: ");
            scanf("%s", name);
            for (int i = 0; i < len; i++) 
            {
                if (p[i].age != 0 && !strcmp(p[i].name, name)) 
                {
                    found_index = i;
                    if (++count > 1) return -2;
                }
            }
            break;
        case 3:
            printf("Enter tel to find: ");
            scanf("%s", tel);
            for (int i = 0; i < len; i++) 
            {
                if (p[i].age != 0 && !strcmp(p[i].tel, tel)) 
                {
                    found_index = i;
                    if (++count > 1) return -2;
                }
            }
            break;
        case 4:
            printf("Enter age to find: ");
            scanf("%d", &age);
            for (int i = 0; i < len; i++) 
            {
                if (p[i].age != 0 && p[i].age == age) 
                {
                    found_index = i;
                    if (++count > 1) return -2;
                }
            }
            break;
        default:
            printf("Invalid way\n");
            return -1;
    }
    return found_index;//if(count >1) return-2; ,count = 1,return find_index,count=0 return-1;
}

void MOD_STU(stu* pm) 
{
    int choose;
    char newname[10], newtel[12];
    int newid, newage;

    printf("Current info: ");
    PRINT_ONE(pm);
    printf("Modify options:1.id 2.age 3.name 4.tel, other key to exit: ");
    scanf("%d", &choose);

    switch (choose) 
    {
        case 1:
            printf("New id: ");
            scanf("%d", &newid);
            pm->id = newid;
            printf("Modified id: %d\n", pm->id);
            break;
        case 2:
            printf("New age: ");
            scanf("%d", &newage);
            pm->age = newage;
            printf("Modified age: %d\n", pm->age);
            break;
        case 3:
            printf("New name: ");
            clear();
            scanf("%s", newname);
            strcpy(pm->name, newname);
            printf("Modified name: %s\n", pm->name);
            break;
        case 4:
            printf("New tel: ");
            scanf("%s", newtel);
            strcpy(pm->tel, newtel);
            printf("Modified tel: %s\n", pm->tel);
            break;
        default:
            printf("Exit modification\n");
    }
}

void DELET_STU(stu* p1, int del_index, int len1) 
{
    int j= 0;
    for ( j = del_index; j < len1 - 1; j++) 
    {
        p1[j] = p1[j + 1];
    }
    p1[len1 - 1].id = 0;
    p1[len1 - 1].age = 0;
    memset(p1[len1 - 1].name, 0, 10);
    memset(p1[len1 - 1].tel, 0, 12);
    printf("Delete successful\n");
    student_count--;
}

void PRINT_ONE(stu* p) 
{
    printf("id:%d name:%s age:%d tel:%s\n", p->id, p->name, p->age, p->tel);
}

void PRINT_ALL(stu* p, int len) 
{
    int count = 0;
    printf("\n===== All  %d Students =====\n",student_count);
    for (int i = 0; i < len; i++) 
    {
        if (p[i].age != 0) 
        {
            printf("No.%d: ", ++count);//count = i+1
            PRINT_ONE(&p[i]);
        }
    }
    if (count == 0) printf("Address book is empty\n");// count =student number
}


void handleAdd(stu* gec, int len) 
{
    ADD_STU(gec, len);
}

void handleModify(stu* gec, int len) 
{
    int way, find_index;
    printf("Choose way to MOD:\n1.id 2.name 3.tel 4.age, enter: ");
    scanf("%d", &way);
    find_index = FIND_STU(gec, way, len);

    if (find_index == -1)
    {
        printf("Student not found\n");
    } 
    else if (find_index == -2)
    {
        printf("results>1,one time MOD one stu\n");
    } 
    else 
    {
        MOD_STU(&gec[find_index]);
    }
}

void handleDelete(stu* gec, int len) 
{
    int way, find_index;
    printf("删除方式:1.id 2.name 3.tel 4.age, enter: ");
    scanf("%d", &way);
    find_index = FIND_STU(gec, way, len);

    if (find_index == -1) 
    {
        
        printf("Student not found\n");
        
    } 
    else if (find_index == -2) 
    {
        printf("Multiple results, cannot delete\n");
    } 
    else 
    {
         printf("Find only one student\n");
        printf("1.sure delele--------any other key to cancle\n");
        scanf("%d",&way);
        if(way == 1)
        DELET_STU(gec, find_index, len);
        
    }
}

void handlePrintAll(stu* gec, int len) 
{
    PRINT_ALL(gec, len);
}
void handleFind(stu* gec, int len) //最核心
{
    int way;
    int id;int age;          // 存储用户输入的查找条件和值
    char name[10], tel[12];
    int count = 0;       // 匹配的学生数量
    stu* results[len];   // 存储所有匹配的学生指针（最多len个）

    // 1. 提示并获取查找方式
    printf("Choose ways to find:1.id 2.name 3.tel 4.age, enter: ");
    scanf("%d", &way);

    // 2. 根据查找方式，读取输入的条件，并遍历数组收集匹配项
    switch (way) {
        case 1: // 按ID查找
            printf("Enter id to find: ");
            scanf("%d", &id);
            for (int i = 0; i < len; i++) 
            {
                if (gec[i].age != 0 && gec[i].id == id) 
                { // 非空且ID匹配
                    results[count++] = &gec[i];//存符合条件的学生的指针进里头
                }
            }
            break;
        case 2: // 按姓名查找
            printf("Enter name to find: ");
            scanf("%s", name);
            for (int i = 0; i < len; i++) {
                if (gec[i].age != 0 && strcmp(gec[i].name, name) == 0) 
                { // 非空且姓名匹配
                    results[count++] = &gec[i];
                }
            }
            break;
        case 3: // 按电话查找
            printf("Enter tel to find: ");
            scanf("%s", tel);
            for (int i = 0; i < len; i++) {
                if (gec[i].age != 0 && strcmp(gec[i].tel, tel) == 0) 
                { // 非空且电话匹配
                    results[count++] = &gec[i];
                }
            }
            break;
        case 4: // 按年龄查找
            printf("Enter age to find: ");
            scanf("%d", &age);
            for (int i = 0; i < len; i++) {
                if (gec[i].age != 0 && gec[i].age == age) 
                { // 非空且年龄匹配
                    results[count++] = &gec[i];
                }
            }
            break;
        default:
            printf("No this way\n");
            return; // 无效方式，直接退出
    }
    if (count == 0) 
    {
        printf("Student not found\n");
    } 
    else if (count == 1) 
    {
        printf("Found 1 result:\n");
        PRINT_ONE(results[0]); // 打印唯一结果
    } 
    else 
    {
        printf("Found %d results:\n", count);
        for (int i = 0; i < count; i++) { // 遍历打印所有匹配结果
            PRINT_ONE(results[i]);
        }
    }
}

void menu() 
{
    printf("\n===== Address Book System =====\n");
    printf("1.Add 2.Modify 3.Delete 4.Find 5.Print all, other key to exit\n");
    printf("Enter choice: ");
}